package com.zengguang.testkaoshi.controller;

import com.zengguang.testkaoshi.eo.RoleEOPage;
import com.zengguang.testkaoshi.pojo.Role;
import com.zengguang.testkaoshi.services.impl.RoleService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "role")
@RestController
@CrossOrigin
public class RoleAction {
    @Autowired
    private RoleService service;

    @ApiOperation(value = "角色添加方法")
    @RequestMapping(value = "/addRole",method = RequestMethod.GET)
    public ResponseMessage add(Role role){
        int isAdd=0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (role!=null) {
            //(2）角色名称不能为空
            if (role.getRolename() != null) {
                //添加角色数据
                isAdd = service.insertRole(role);
                if (isAdd==1){
                    return Result.success("保存成功");
                }else if (isAdd==-1){
                    return Result.error("角色重复不允许添加!");
                }else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }

    @ApiOperation(value = "角色删除方法")
    @RequestMapping(value = "/delRole",method = RequestMethod.GET)
    public ResponseMessage delRole(int roleid){
        if (roleid!=0){
            int delInt=0;
            delInt=service.deleteRole(roleid);
            if (delInt>0){
                return Result.success("删除成功");
            }else {
                return Result.error("删除失败");
            }
        }else {
            return Result.error("删除id不能为0");
        }
    }

    @ApiOperation(value = "角色更新方法")
    @RequestMapping(value = "/updateRole",method = RequestMethod.GET)
    public ResponseMessage updateRole(Role role){
        //传入参数不能为空
        if (role!=null){
            //传入id不能为0
            if (role.getRoleid()!=0){
                if (service.updateRole(role)==1){
                    return Result.success("更新成功");
                }else {
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "查询全部")
    @RequestMapping(value = "/getRoleAll",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    public ResponseMessage getAll(RoleEOPage roleEOPage){
        Map<String, List> map=new HashMap<>();
        List<RoleEOPage> list=service.getWhereAll(roleEOPage);
        map.put("list",list);
        return Result.success("200",map);
    }

}
